package servlet;

import Utils.PageUitls;
import bean.Student;
import Utils.FileUploadUtils;
import service.StudentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/student")
@MultipartConfig
public class StudentServlet extends BaseServlet {
    private StudentService ss = new StudentService();


    public void addStudent(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String sname = req.getParameter("sname");
        String sex = req.getParameter("sex");
        String age = req.getParameter("age");
        String degree = req.getParameter("degree");

        String mark = req.getParameter("mark");
        //获取上传头像名称
        Part part = req.getPart("pic");
        //调用工具类中上传方法
        String pic = FileUploadUtils.uploadPic(part);
        Student stu = new Student(sname, sex, Integer.parseInt(age), degree, pic, mark);
        int i = ss.addStudent(stu);
        PrintWriter out = resp.getWriter();
        if (i > 0) {
          //  out.print("成功");
            resp.sendRedirect("student?method=findAllStudent");
        } else {
            //out.print("失败");
            resp.sendRedirect("addStudent.html");
        }
    }
    public void findAllStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        int totalCounnt = ss.getTotalCounnt();
        String currentPage = req.getParameter("currentPage");
        PageUitls pu=new PageUitls(totalCounnt,5 ,currentPage );
        List<Student> list=ss.findALLStudent(pu);
        pu.setList(list);
        req.setAttribute("pu",pu );
        req.getRequestDispatcher("index.jsp").forward(req,resp );

    }

    public void findStudentBySid(HttpServletRequest req,HttpServletResponse resp)throws Exception{
        String sid=req.getParameter("sid");
        Student student = ss.findStudentBySid(Integer.parseInt(sid));
        req.setAttribute("stu",student );
        req.getRequestDispatcher("details.jsp").forward(req ,resp );

    }
    public void updateToshow(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取学号
        String sid = req.getParameter("sid");
        //调用serivce
        Student student = ss.findStudentBySid(Integer.parseInt(sid));
        //存域
        req.setAttribute("stu",student);
        //跳转
        req.getRequestDispatcher("updatestudent.jsp").forward(req,resp);
    }
    public void updateStudent(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //获取页面传递过来数据
        String sname = req.getParameter("sname");
        String sex = req.getParameter("sex");
        String age = req.getParameter("age");
        String degree = req.getParameter("degree");
        String pic = req.getParameter("oldpic");
        String mark = req.getParameter("mark");
        String sid = req.getParameter("sid");
        //上传
        Part part = req.getPart("pic");
        if (part.getSize()!=0){
            pic= FileUploadUtils.uploadPic(part);
        }
        //封装对象
        Student stu = new Student(Integer.parseInt(sid),sname,sex,Integer.parseInt(age),degree,pic,mark);
        //调用service
        int row = ss.updateStudent(stu);
        //判断
        if (row>0){
            resp.sendRedirect("student?method=findAllStudent");
        }
    }
    public void deleteStudent(HttpServletRequest req , HttpServletResponse resp)throws Exception{
        String sid=req.getParameter("sid");
        Student stu = new Student(Integer.parseInt(sid));
        int delete = ss.deleteStudent(stu);
        if (delete>0){
            resp.sendRedirect("student?method=findAllStudent");
        }
    }
    public void checkSname(HttpServletRequest req,HttpServletResponse resp)throws Exception{
        String sname = req.getParameter("sname");
        Student student = ss.checkSname(sname);
        PrintWriter writer = resp.getWriter();
        if(student==null){
            writer.print(false);
        }else {
            writer.print(true);
        }
    }
}
